﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ApplicationLibrary;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.SqlConnector;

using Vintner.Database;
using Vintner.Library;

namespace Vintner.Forms
{
    public partial class Items : ApplicationLibrary.DBManager.DocumentForm
    {
        public Items()
        {
            InitializeComponent();
        }

        public Items(GlobalInfo _gInfo)
            :base(_gInfo, "Items")
        {
            InitializeComponent();
        }

        #region Virtual Method
        public override string DefineBrowseQuery()
        {
            QueryBuilder qb = new QueryBuilder();

            qb.Clear();
            qb.AddSelectAll(VI_Items.Name);
            qb.AddFrom(VI_Items.Name);

            return qb.Query;
        }

        public override string GetCodeColumn()
        {
            return VI_Items.Item.Name;
        }

        public override Table GetRadarTable()
        {
            return new VI_Items();
        }

        protected override void OnAttachData()
        {
            dbManager = new dbmanagerItems("itemsForm", base.providerType, ConnectionString);
            dbManager.AddMaster(this, VI_Items.Name, true);
            dbManager.AddSlave(VI_ItemsPrice.Name, true);
            dbManager.AddRelation("ItemPrice", VI_Items.Item, VI_ItemsPrice.Item);
            BindControl(dgwPrice);

            dgwPrice.AutoGenerateColumns = false;
            dgwPrice.DataSource = dbManager.SlaveBinding("ItemPrice");
        }

        protected override void OnBindData()
        {
            BindControl(txtCode, VI_Items.Item);
            BindControl(txtDescription, VI_Items.Description);
            BindControl(ntbSize, VI_Items.Size);
            BindControl(ckbDisabled, VI_Items.Disabled, "DBChecked");

            prc_Category.DataPropertyName = VI_ItemsPrice.Category.Name;
            prc_Price.DataPropertyName = VI_ItemsPrice.Price.Name;
            prc_TaxCode.DataPropertyName = VI_ItemsPrice.TaxCode.Name;
            //prc_Disabled.DataPropertyName = VI_ItemsPrice.Disabled.Name;
                

            if (dbManager.GetDataColumn(VI_Items.Disabled).DefaultValue is System.DBNull)
                dbManager.GetDataColumn(VI_Items.Disabled).DefaultValue = false;
        }

        protected override void OnDisableControlsForEdit()
        {
            txtCode.Enabled = false;
            base.OnDisableControlsForEdit();
        }

        protected override void FocusOnNew()
        {
            txtCode.Focus();
        }

        protected override bool OnBeforeSave()
        {
            return base.OnBeforeSave();
        }
        #endregion
    }

    #region DbManager dbmanagerItems
    /// <summary>
    /// Classe derivata per gestioni particolari
    /// </summary>
    internal class dbmanagerItems : DBManager
    {
        public dbmanagerItems(string name, ProviderType providerType, string conString)
            : base(name, providerType, conString)
        {
        }

        protected override void dAdapter_MasterRowUpdating(object sender, SqlABRowUpdatingEventArgs e)
        {
            base.dAdapter_MasterRowUpdating(sender, e);
        }

        protected override void dAdapter_RowUpdating(object sender, SqlABRowUpdatingEventArgs e)
        {
            if (e.StatementType == StatementType.Insert)
            {
                e.Row[VI_ItemsPrice.Item.Name] = GetColumn(VI_Items.Item, Pos);
            }
            base.dAdapter_RowUpdating(sender, e);
        }

        protected override string CreateMasterQuery()
        {
            this.Table = new VI_Items();
            QueryBuilder qB = new QueryBuilder();
            qB.AddSelect();
            qB.AddFrom(this.Table);
            qB.AddCompare(this.Table.ForeignKey, "@p1");

            return qB.Query;
        }

        protected override List<SqlABParameter> CreateMasterParam()
        {
            List<SqlABParameter> PList = new List<SqlABParameter>();

            SqlABParameter sParam = new SqlABParameter(providerType, "@p1", SqlDbType.NVarChar, 21);
            sParam.Value = "";
            PList.Add(sParam);
            return PList;
        }

        protected override string CreateSlaveQuery(string name)
        {
            if (name == VI_ItemsPrice.Name)
            {
                VI_ItemsPrice ItemsPrice = new VI_ItemsPrice();
                QueryBuilder qB = new QueryBuilder();
                qB.AddSelect();
                qB.AddFrom(VI_ItemsPrice.Name);
                qB.AddCompare(VI_ItemsPrice.Item, "@p2");
                qB.AddOrder(VI_ItemsPrice.Category);
                return qB.Query;
            }

            return "";
        }

        protected override List<SqlABParameter> CreateSlaveParam(string name)
        {
            if (name == VI_ItemsPrice.Name)
            {
                List<SqlABParameter> PList = new List<SqlABParameter>();

                SqlABParameter sParam = new SqlABParameter(providerType, "@p2", SqlDbType.NVarChar, 21);
                sParam.Value = "";
                PList.Add(sParam);
                return PList;
            }
            return null;
        }
    }
    #endregion
}
